/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Welcome;

import RMS.ScoreRMS;
import ReadXML.ImageInfo;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

public class ScoreSave extends Canvas {

    private Image bufferImage;
    private Start midlet;
    private boolean is, isGameOver;
    private int instanceKeyCode, score;
    private Font f;

    public ScoreSave(Start midlet, boolean isGameOver, int score) {
        this.midlet = midlet;
        this.score = score;
        this.setFullScreenMode(true);
        this.isGameOver = isGameOver;
        bufferImage = Image.createImage(getWidth(), getHeight());
        DrawChoice.liv = 3;
    }

    protected void paint(Graphics g) {
        render();
        g.drawImage(bufferImage, 0, 0, Graphics.LEFT | Graphics.TOP);
    }

    private void render() {
        Graphics g = bufferImage.getGraphics();
        g.setColor(0x00FCF93A);
        g.fillRect(0, 0, getWidth(), getHeight());
        f = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_MEDIUM);
        g.setFont(f);
        g.setColor(0x00EA9414);
        g.fillRect(0, 0, getWidth(), f.getHeight() + 4);
        g.fillRect(0, getHeight() - f.getHeight() - 4, getWidth(), f.getHeight() + 4);
        g.setColor(0xffffff);
        g.drawString("Lưu điểm", 2, 2, Graphics.LEFT | Graphics.TOP);
        g.drawLine(0, getHeight() - f.getHeight() - 4, getWidth(), getHeight() - f.getHeight() - 4);
        if (!is) {
            g.drawString("Nhập tên", getWidth() - 1, getHeight() - 2, Graphics.RIGHT | Graphics.BOTTOM);
        } else {
            g.drawString("OK", getWidth() - 1, getHeight() - 2, Graphics.RIGHT | Graphics.BOTTOM);
        }
        g.drawString("Trở lại", 2, getHeight() - 2, Graphics.LEFT | Graphics.BOTTOM);

        f = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_MEDIUM);
        g.setFont(f);
        g.setColor(0x000000);
        g.drawString("Điểm của bạn là " + String.valueOf(score), 2, f.getHeight() + 6, Graphics.LEFT | Graphics.TOP);
        g.drawString("Nhập tên của bạn", 2, 2 * f.getHeight() + 8, Graphics.LEFT | Graphics.TOP);
        g.setColor(0xffffff);
        g.fillRect(2, 3 * f.getHeight() + 10, getWidth() - 4, f.getHeight() + 4);
        g.setColor(255, 0, 0);
        f = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_MEDIUM);
        g.setFont(f);
        g.drawRect(2, 3 * f.getHeight() + 10, getWidth() - 4, f.getHeight() + 4);
        g.drawString(PassTextboxText.getTextBoxText(), 5, 3 * f.getHeight() + 12, Graphics.LEFT | Graphics.TOP);
    }

    protected void pointerPressed(int x, int y) {
        f = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_MEDIUM);
        if (y <= getHeight() && y >= getHeight() - f.getHeight() - 10) {
            if (x <= getWidth() && x >= getWidth() - f.stringWidth("Nhập tên") - 10) {
                if (!is) {
                    is = true;
                    midlet.disManager.pushDisplayable(this);
                    InsertAnswer ia = new InsertAnswer(midlet);
                    midlet.disManager.pushDisplayable(ia.insertAnswer(""));
                } else {
                    if (PassTextboxText.getTextBoxText().equals("")) {
                        is = false;
                        Alert alert = new Alert("", "Bạn phải nhập tên để lưu điểm", null, AlertType.INFO);
                        alert.setTimeout(Alert.FOREVER);
                        midlet.getDis().setCurrent(alert, this);
                    } else {
                        ScoreRMS scoreRecord = new ScoreRMS();
                        scoreRecord.createRecord(PassTextboxText.getTextBoxText(), score);
                        PassTextboxText.setTextBoxText("");
                        DrawChoice.score = 0;
                        midlet.getDis().setCurrent(new HighScore(midlet));
                    }
                }
            } else if (x >= 0 && x <= f.stringWidth("Trở lại") + 10) {
                if (DrawChoice.liv == 0 || isGameOver) {
                    DrawChoice.score = 0;
                    midlet.disManager.home();
                } else {
                    midlet.disManager.popDisplayable();
                }
            }
        }
    }

    protected void keyPressed(int keyCode) {
        instanceKeyCode = KeyCodeAdapter.getInstance().adoptKeyCode(keyCode);

        switch (instanceKeyCode) {
            case KeyCodeAdapter.KEY_3:
            case KeyCodeAdapter.SOFT_KEY_RIGHT:
                if (!is) {
                    is = true;
                    midlet.disManager.pushDisplayable(this);
                    InsertAnswer ia = new InsertAnswer(midlet);
                    midlet.disManager.pushDisplayable(ia.insertAnswer(""));
                } else {
                    if (PassTextboxText.getTextBoxText().equals("")) {
                        is = false;
                        Alert alert = new Alert("", "Bạn phải nhập tên để lưu điểm", null, AlertType.INFO);
                        alert.setTimeout(Alert.FOREVER);
                        midlet.getDis().setCurrent(alert, this);
                    } else {
                        ScoreRMS scoreRecord = new ScoreRMS();
                        scoreRecord.createRecord(PassTextboxText.getTextBoxText(), score);
                        PassTextboxText.setTextBoxText("");
                        DrawChoice.score = 0;
                        midlet.getDis().setCurrent(new HighScore(midlet));
                    }
                }
                break;

            case KeyCodeAdapter.KEY_1:
            case KeyCodeAdapter.SOFT_KEY_LEFT:
                if (DrawChoice.liv == 0 || isGameOver) {
                    DrawChoice.score = 0;
                    midlet.disManager.home();
                } else {
                    midlet.disManager.popDisplayable();
                }
                break;
        }
    }
}
